import {
	fileUrl
} from '@/core/config.js'

export default {
	imgPath(url, compress = true) {
		if (!url) return

		if (url.includes('http://') || url.includes('https://')) {
			return url
		} else if (compress) {
			return fileUrl + '/' + url + '?x-oss-process=image/resize,w_500,m_lfit'
		} else {
			return fileUrl + '/' + url
		}
	},

	getAvatarPath(url) {
		if (!url) {
			return '/static/logo.png'
		}

		return this.imgPath(url)
	},
	//预览图片
	previewImage(urls, current = 0) {

		for (var index = 0; index < urls.length; index++) {
			urls[index] = this.imgPath(urls[index], false);
		}

		uni.previewImage({
			urls,
			current
		});
	},

	//调转登录页面
	doLogin() {
		// #ifdef MP-WEIXIN
		uni.navigateTo({
			url: '/pages/user/wx-login'
		});
		// #endif

		// #ifndef MP-WEIXIN
		uni.navigateTo({
			url: '/pages/user/account-login'
		});
		// #endif
	},

	//返回上一页面，没有上一页面则返回首页
	navigateBack() {
		const pages = getCurrentPages();
		if (pages.length > 1) {
			uni.navigateBack();
		} else if (pages.length === 1) {
			uni.switchTab({
				url: '/pages/index/index'
			});
		}
	}
}